<template>
    <div class="option">
        <!--分享-->
        <div class="drop-layout" v-if="isShowShareUser">
            <dialog-share-user :data="data"/>
        </div>
        <!--编辑-->
        <div class="drop-layout" v-if="isShowEdit">
            <dialog-add-view @callback="editFile" :data="data" :is-edit="true"/>
        </div>
        删除
        <div class="drop-layout" v-if="isShowConfirm">
            <dialog-confirm-view @callback="deleteFile"/>
        </div>
    </div>
</template>

<script>
    import DialogShareUser from '../../common/DialogShareUser';
    import DialogAddView from '../../common/DialogAddView';
    import DialogConfirmView from '../../common/DialogConfirmView';

    import { fileRename, mindMapRename, fileDelete, cancleDdit, applyDdit,  } from '../../../api/file'
    import { folderRename, folderDelete } from '../../../api/folder'
    import { folderJudge1, fileJudge1, mindMapJudge1 } from './judge'

    export default {
        name: 'OptionView',
        components: {
            DialogShareUser,
            DialogAddView,
            DialogConfirmView
        },
        props:{
            isShowShareUser: Boolean,
            isShowEdit: Boolean,
            data: Object,
            isShowConfirm: Boolean
        },
        data() {
            return {

            }
        },
        computed: {
            authority() {
                return this.$store.state.index.authority
            },
        },
        methods: {
            async editFile(type,content) {
                if (type === '2' && content === '') {
                    return;
                }
                if(type === '1') {
                    this.$parent.isShowEdit = false;
                    this.$parent.isShowOption = false;
                    this.$parent.isShowShareUser = false;
                }else {
                    if (this.authority.isUpdate === 0) {
                        this.$toast('无操作权限')
                        return false
                    }
                    if(this.data.suffix) {
                        if(this.data.suffix === 'html') {
                            const is = await fileJudge1(0, content)
                            if(is)
                                this.fileRename(content)
                        } else {
                            const is = await mindMapJudge1(0, content)
                            if(is)
                                this.mindMapRename(content)
                        }
                    } else {
                        const parentId = this.data ? this.data.parentId : 0;
                        const is = await folderJudge1(parentId, content);
                        if(is)
                            this.folderRename(content)
                    }
                    this.$parent.isShowEdit = false;
                    this.$parent.isShowOption = false;
                    this.$parent.isShowShareUser = false;
                }
            },
            fileRename(content) {
                const name = content + '.' + this.data.suffix
                const parentId = this.data.parentId
                const folderId = this.data.id
                fileRename(parentId, name, folderId)
                    .then(() => {
                        this.data.name = content;
                        this.$toast('修改成功');
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
            mindMapRename(content) {
                const name = content
                const parentId = this.data.parentId
                const folderId = this.data.id
                mindMapRename(parentId, name, folderId)
                    .then(() => {
                        this.data.name = content;
                        this.$toast('修改成功');
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
            folderRename(content) {
                const parentId = this.data.parentId
                const folderId = this.data.id
                folderRename(parentId, content, folderId)
                    .then(() => {
                        this.data.name = content;
                        this.$toast('修改成功');
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
        //    删除
            deleteFile(type) {
                if(type === '1') {
                    this.$parent.isShowConfirm = false;
                    return;
                } else {
                    if (this.authority.isUpdate === 0) {
                        this.$toast('无操作权限')
                        return false
                    }
                    if(this.data && this.data.suffix)
                        this.fileDelete()
                    else
                        this.folderDelete()
                }

            },
            fileDelete() {
                const id = this.data.id;
                fileDelete(id)
                    .then(() => {
                        this.$parent.fileList.splice(this.$parent.fileList.findIndex(v => v.id === id), 1);
                        if (id === this.$store.state.index.selectedFile.id) {
                            this.$store.commit('index/set_selectedFile', { suffix: '' })
                        }
                        this.$toast('删除成功')
                        this.$parent.isShowEdit = false;
                        this.$parent.isShowOption = false;
                        this.$parent.isShowShareUser = false;
                        this.$parent.isShowConfirm = false;
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
            folderDelete() {
                const id = this.data.id;
                folderDelete(id)
                    .then(() => {
                        this.$parent.folderList.splice(this.$parent.folderList.findIndex(v => v.id === id), 1);
                        this.$toast('删除成功')
                        this.$parent.isShowEdit = false;
                        this.$parent.isShowOption = false;
                        this.$parent.isShowShareUser = false;
                        this.$parent.isShowConfirm = false;
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
            //申请编辑
            applyDdit() {
                const fileid = this.data.id
                applyDdit({ fileid })
                    .then(result => {
                        const { data } = result
                        if (data) {
                            this.data.editor = JSON.parse(localStorage.getItem('userInfo')).realName
                            console.log(result)
                            this.$store.commit('index/set_selectedFile', { suffix: '' })

                            this.$nextTick(() => {
                                this.$store.commit('index/set_selectedFile', this.data)
                            })
                        } else {
                            this.$toast(result.msg)
                        }
                        this.$toast('申请编辑权限成功');
                        this.$parent.isShowEdit = false;
                        this.$parent.isShowOption = false;
                        this.$parent.isShowShareUser = false;
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
            //释放编辑
            cancleDdit() {
                const fileid = this.data.id
                cancleDdit({ fileid })
                    .then(result => {
                        this.data.editor = ''
                        this.$store.commit('index/set_selectedFile', { suffix: '' })
                        this.$nextTick(() => {
                            this.$store.commit('index/set_selectedFile', this.data)
                        })
                        console.log(result)
                        this.$toast('释放编辑权限成功');
                        this.$parent.isShowEdit = false;
                        this.$parent.isShowOption = false;
                        this.$parent.isShowShareUser = false;
                    })
                    .catch(err => {
                        console.log(err)
                    })
            },
        }
    }
</script>

<style scoped>
    .drop-layout {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background: rgba(0,0,0,.3);
        z-index: 9999;
    }
</style>